package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.News;
import com.mars.module.admin.entity.UserComment;
import com.mars.module.admin.mapper.NewsMapper;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.UserCommentRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.UserCommentMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.UserComment;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IUserCommentService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户评论业务层处理
 *
 * @author mars
 * @date 2024-03-04
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserCommentServiceImpl implements IUserCommentService {

    private final UserCommentMapper baseMapper;
    private final NewsMapper newsMapper;
    private final SysUserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserComment add(UserCommentRequest request) {
        UserComment entity = UserComment.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setCreateById(ContextUserInfoThreadHolder.get().getId());
        entity.setUserId(ContextUserInfoThreadHolder.get().getId());
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(UserCommentRequest request) {
        UserComment entity = UserComment.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public UserComment getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<UserComment> pageList(UserCommentRequest request) {
        Page<UserComment> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<UserComment> query = this.buildWrapper(request);
        IPage<UserComment> pageRecord = baseMapper.selectPage(page, query);
        List<UserComment> records = pageRecord.getRecords();
        List<UserComment> recordsNew = new ArrayList<>();
        for (UserComment collection : records) {
            News news = newsMapper.selectById(collection.getNewsId());
            SysUser sysUser = userMapper.selectById(collection.getUserId());
            collection.setNewTitle(news == null ? "" : news.getTitle());
            collection.setUserName(sysUser == null ? "" : sysUser.getUserName());
            recordsNew.add(collection);
        }
        if (StringUtils.isNotBlank(request.getNewTitle())) {
            recordsNew = recordsNew.stream().filter(e -> e.getNewTitle().contains(request.getNewTitle())).collect(Collectors.toList());
        }
        if (StringUtils.isNotBlank(request.getContent())) {
            recordsNew = recordsNew.stream().filter(e -> e.getContent().contains(request.getContent())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<UserComment> list(UserCommentRequest request) {
        LambdaQueryWrapper<UserComment> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<UserComment> buildWrapper(UserCommentRequest param) {
        LambdaQueryWrapper<UserComment> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getContent())) {
            query.like(UserComment::getContent, param.getContent());
        }
        if (param.getNewsId() != null) {
            query.eq(UserComment::getNewsId, param.getNewsId());
        }
        return query;
    }

}
